package top.kaoshanji.p3ch22c;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码清单-p3ch22c-269页a：被观察者实现类
 * @author kaoshanji
 * ^_^
 * create time 2025/3/26 16:25
 */
public class HanFeiZi implements Observable {

    // 实例属性被隐藏起来...
    // 通过方法来操作...get/set
    private List<Observer> observerList = new ArrayList<>();

    @Override
    public void addObserver(Observer observer) {
        this.observerList.add(observer);
    }

    @Override
    public void deleteObserver(Observer observer) {
        this.observerList.remove(observer);
    }

    @Override
    public void notifyObservers(String context) {
        for (Observer observer: observerList) {
            observer.update(context);
        }
    }

    public void haveBreakfast() {
        System.out.println("韩非子：开始吃饭了...");
        // 通知所有的观察者
        this.notifyObservers("韩非子在吃饭");
    }

    public void haveFun() {
        System.out.println("韩非子：开始娱乐了...");
        this.notifyObservers("韩非子在娱乐");
    }


}
